package linuxbox.reservation.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Iterator;
import java.text.SimpleDateFormat;

import linuxbox.BoxStatic;
import linuxbox.resource.logic.*;
import linuxbox.reservation.logic.*;

/**
 * This class displays a table that contains the resources being reserved
 */
public class ResourceTimeList extends TagSupport
{
  protected int resourceID = -1;
  protected long day;
  protected int timeInterval = 20;
  protected int startHour = 8;
  protected int endHour = 17;
  protected TimeZone timeZone = null;

  protected static BoxReservation home = BoxReservation.getInstance();
  protected static BoxResource resourceHome = BoxResource.getInstance();

  public int doStartTag() throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut();

      int cols = 3;
      if (timeInterval >0 && timeInterval < 60)
        cols = 60 / timeInterval;
      else
        timeInterval = 20;
      Calendar date = Calendar.getInstance(timeZone);
      date.setTimeInMillis(day);
//      java.util.Date date = new java.util.Date(day);

      SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(BoxStatic.VERYLONG_DATE_FORMAT, Locale.US);
      dateTimeFormatter.setCalendar(Calendar.getInstance(timeZone));
/*
          out.print("<table cellspacing=1 border=1 width=\"100%\">");
      out.print("<tr><td colspan="+ (cols*(endHour-startHour)-3) + ">");
      out.print("<table cellspacing=0 border=0 width=\"100%\"><tr><td><b>");

      SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(BoxStatic.VERYLONG_DATE_FORMAT, Locale.US);
      dateTimeFormatter.setCalendar(Calendar.getInstance(timeZone));

      out.print(dateTimeFormatter.format(date.getTime()));
      out.print("</b></td><td align=center bgcolor=#ddddff>Free</td><td>&nbsp;</td><td align=center bgcolor=#ff0000>Busy</td></tr></table>");

      out.print("</td></tr>");
*/
      out.print("<tr>");
      Calendar tmp = Calendar.getInstance(timeZone);
      tmp.set(Calendar.HOUR_OF_DAY,startHour);
      dateTimeFormatter.applyPattern(BoxStatic.SHORT_HOUR_FORMAT);
      for(int i=startHour; i < endHour; i++)
      {
        out.print("<td align=center colspan="+ cols);
        out.print("><FONT size=-2>" + dateTimeFormatter.format(tmp.getTime()).toLowerCase());
        tmp.add(Calendar.HOUR_OF_DAY, 1);
        out.print("-" + dateTimeFormatter.format(tmp.getTime()).toLowerCase());
        out.print("</FONT></td>");
      }
      out.print("</tr>");

      Iterator it = home.getEntries(date, null, false, resourceID, "fromtime", true);

      out.print("<tr>");

      tmp.setTime(date.getTime());
      tmp.set(Calendar.HOUR_OF_DAY,startHour);
      tmp.set(Calendar.MINUTE,0);
      tmp.set(Calendar.SECOND,0);
      tmp.set(Calendar.MILLISECOND,0);

      int emptyCols = 0;
      int entryCols = 0;
      Calendar entryFromTime = Calendar.getInstance(timeZone);
      Calendar entryToTime = Calendar.getInstance(timeZone);
      BoxReservationEntry entry = null;
      cols = (endHour-startHour)*60/timeInterval;
      int curYear = tmp.get(Calendar.YEAR);
      int curDate = tmp.get(Calendar.DAY_OF_YEAR);
      for(int i=0; i < cols; i++)
      {
        if (it != null && it.hasNext() && entry == null)
        {
          entry = (BoxReservationEntry)it.next();
          entryFromTime.setTime(entry.getFromTime());
          if ( !entry.getRepeat()
            && entryFromTime.get(Calendar.YEAR) <= curYear
            && entryFromTime.get(Calendar.DAY_OF_YEAR) < curDate )
          {
            entryFromTime = (Calendar) tmp.clone();
            entryFromTime.set(Calendar.HOUR_OF_DAY,0);
          }
          else //assert fromtime is always greater than curDate
          {
            entryFromTime.set(Calendar.YEAR, curYear);
            entryFromTime.set(Calendar.DAY_OF_YEAR, curDate);
          }
          if ( !entry.getRepeat()
            && entryToTime.get(Calendar.YEAR) >= curYear
            && entryToTime.get(Calendar.DAY_OF_YEAR) > curDate )
          {
            entryToTime = (Calendar) tmp.clone();
            entryToTime.set(Calendar.HOUR_OF_DAY,23);
            entryToTime.set(Calendar.MINUTE,59);
            entryToTime.set(Calendar.SECOND,59);
          }
          else
          {
            entryToTime.setTime(entry.getToTime());
            entryToTime.set(Calendar.YEAR, curYear);
            entryToTime.set(Calendar.DAY_OF_YEAR, curDate);
          }
        }
        if ( entry == null )
        {
          out.print("<td align=center bgcolor=#ddddff colspan=");
          out.print(cols-i);
          out.print(">&nbsp;</td>");
          emptyCols = 0;
          entryCols = 0;
          break;
        }

        if ( emptyCols > 0 )
        {
          if ( tmp.before(entryFromTime))
          {
            emptyCols++;
          }
          else
          {
            out.print("<td align=center bgcolor=#ddddff colspan=");
            out.print(emptyCols);
            out.print(">&nbsp;</td>");
            emptyCols = 0;
            entryCols =0;
          }
        }
        else if ( entryCols > 0)
        {
          if ( tmp.before(entryToTime))
            entryCols++;
          else
          {
            out.print("<td align=center bgcolor=#ff0000 colspan=");
            out.print(entryCols);
            out.print(">"+entry.getSubject()+"</td>");

            emptyCols = 0;
            entryCols = 0;
            entry = null;
          }
        }


        if ( entryCols == 0 && emptyCols == 0 && entry == null)
        {
          //this is the case similiar to initial state but not the same
          //we want back i and don't add the tmp
          i --;
        }
        else
        {
          if (entryCols == 0 && emptyCols == 0)
          {
            if ( tmp.before(entryFromTime))
              emptyCols++;
            else
              entryCols++;
          }
          tmp.add(Calendar.MINUTE, timeInterval);
        }
      }
      if ( entryCols > 0)
      {
        out.print("<td align=center bgcolor=#ff0000 colspan=");
        out.print(entryCols);
        out.print(">"+entry.getSubject()+"</td>");
      }
      if ( emptyCols > 0)
      {
        out.print("<td align=center bgcolor=#ddddff colspan=");
        out.print(emptyCols);
        out.print(">&nbsp;</td>");
      }

      out.print(" </tr>");

//      out.print("</table>");
      out.flush();
    }
    catch(Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setResourceID(int aValue)
  {
    resourceID = aValue;
  }

  public void setStartHour(int aValue)
  {
    startHour = aValue;
  }

  public void setEndHour(int aValue)
  {
    endHour = aValue;
  }

  public void setTimeInterval(int aValue)
  {
    timeInterval = aValue;
  }

  public void setDay(long aValue)
  {
    day = aValue;
  }

  public void settimezone(TimeZone tz)
  {
    timeZone = tz;
  }
}
